import { fabric } from "./fabric";
/**
 * 
 * @param {object} obj 
 * @param {Array} setting 
 */
 let exportJson = function (obj,setting){
    if(Array.isArray(setting)){
        let o = {};
        o.version=fabric.version 
        o.objects=[];
        obj._objects.forEach(element => {
          o.objects.push(element.toJSON(setting))
        });
        return o
    }
    return obj.toJSON()
} 
function NumberFormat(objects){
  for(let key in objects){
   typeof objects[key] =="number"?objects[key]:objects[key] =Number.parseInt(objects[key])
  }
  return objects
}
function device(value,name,description){
  this.value=value;
  this.name=name;
  this.description=description
}
/**
 * 在o2内追加o1checked为true的值
 * @param {object} o1 
 * @param {object} o2 
 * @returns {Object} o2
 */
let append = function(o1,o2){
    o2.attr=o2.attr||{}
    for(let item in o1){
        if(o1[item].checked){
            o2.attr[item]=new device(o1[item].value,o1[item].name,o1[item].description) ;
        }
    };
    return o2
}
/**
 * 将转换的json字符串包装增加具名前缀和导出后缀
 * @param {string} json 
 * @param {string} name 
 */
let decoration = function(json){
    return `let deviceinfo = ${json}\nexport default deviceinfo`
}
var _global = typeof window === 'object' && window.window === window
  ? window : typeof self === 'object' && self.self === self
  ? self : typeof global === 'object' && global.global === global
  ? global
  : this
  function click(node) {
    try {
      node.dispatchEvent(new MouseEvent('click'))
    } catch (e) {
      var evt = document.createEvent('MouseEvents')
      evt.initMouseEvent('click', true, true, window, 0, 0, 0, 80,
                            20, false, false, false, false, 0, null)
      node.dispatchEvent(evt)
    }
  }
  /**
   * 保存blob文件
   * @param {Blob} blob 
   * @param {string} name 
   * @param {*} opts 
   */
let  SaveFile = function(blob, name, opts) {
    var URL = _global.URL || _global.webkitURL
    var a = document.createElement('a')
    name = name || blob.name || 'download'
  
    a.download = name
    a.rel = 'noopener' // tabnabbing
  
    // TODO: detect chrome extensions & packaged apps
    // a.target = '_blank'
  
    if (typeof blob === 'string') {
      // Support regular links
      a.href = blob
      if (a.origin !== location.origin) {
        corsEnabled(a.href)
          ? download(blob, name, opts)
          : click(a, a.target = '_blank')
      } else {
        click(a)
      }
    } else {
      // Support blobs
      a.href = URL.createObjectURL(blob)
      setTimeout(function () { URL.revokeObjectURL(a.href) }, 4E4) // 40s
      setTimeout(function () { click(a) }, 0)
    }
  }
  function saveTuopu(canvas){
    let list=[]
    canvas._objects.forEach(element=>{
      if(element instanceof fabric.Line){
        let list1 ={}
        list1.line=element.did
        list1.target= element.target&&element.target.did;
        list1.source= element.source&&element.source.did;
        list.push(list1)
      }
    });
    return list;
  }
  function getdevice(did){
   return this._objects.filter(element=>{
      return element.did===did
    })
  }
  function loadTuopu(tuopulist){
    let that = this;
    tuopulist.forEach(element=>{
      let line = getdevice.call(that,element.line);
      let source=getdevice.call(that,element.source);
      let target =getdevice.call(that,element.target);
      if(line[0]){
        line=line[0];
      line.source=source[0];
      line.target=target[0];
      }
      if(source[0]){
        source = source[0];
        source._sourcelist=source._sourcelist||[];
        source._sourcelist.push(line)
      }
      if(target[0]){
        target = target[0];
        target._targetlist=target._targetlist||[];
        target._targetlist.push(line)
      }
    })
  }
  export {exportJson,SaveFile,append,decoration,loadTuopu,getdevice,saveTuopu}